﻿using Avalonia.Controls;
using Avalonia.Interactivity;
using LuckyFish.ToDo.Data.Model;
using LuckyFish.ToDo.ViewModels;

namespace LuckyFish.ToDo.ToDoViews;

public partial class ToDoView : UserControl
{
    public ToDoView()
    {
        InitializeComponent();

        #region Todo

        TodoBox = this.FindControl<TextBox>("TodoBox");
        CheckBox = this.FindControl<CheckBox>("CheckBox");
        CreatePicker = this.FindControl<DatePicker>("CreatePicker");
        EndPicker = this.FindControl<DatePicker>("EndPicker");

        #endregion

        #region Add Step

        StepBox = this.FindControl<TextBox>("StepBox");
        Picker = this.FindControl<CalendarDatePicker>("Picker");
        StepsItems = this.FindControl<ItemsControl>("StepsItems");

        #endregion
    }

    private void AddStepClick(object? sender, RoutedEventArgs e)
    {
        if (string.IsNullOrEmpty(StepBox.Text) || Picker.SelectedDate == null) return;
        var step = new StepModel
        {
            EndTime = Picker.SelectedDate.Value.ToString("d"),
            Context = StepBox.Text
        };
        var model = FindModel();
        if (model == null) return;
        var key = (DataContext as ToDoModel)!.Key;
        model.Add(step, key);
        Picker.SelectedDate = null;
        StepBox.Text = "";
        if (StepsItems.Items.Count == 0)
            StepsItems.Items.Add(step);
    }

    private void GoBackClick(object? sender, RoutedEventArgs e)
    {
        if (Parent is not ContentControl contentControl) return;
        contentControl.IsVisible = false;
    }

    private void StepUpdateClick(object? sender, RoutedEventArgs e)
    {
        if (sender is not Grid grid) return;
        if (grid.DataContext is not StepModel step) return;
        var _stepContextBlock = grid.FindControl<TextBox>("StepContextBlock");
        var _stepPicker = grid.FindControl<CalendarDatePicker>("StepPicker");
        var _stepCheckBox = grid.FindControl<CheckBox>("StepCheckBox");
        var model = FindModel();
        if (model is null || (string.IsNullOrEmpty(_stepContextBlock!.Text) && _stepPicker!.SelectedDate == null &&
                              _stepCheckBox!.IsChecked == null)) return;
        if (!string.IsNullOrEmpty(_stepContextBlock.Text))
            step.Context = _stepContextBlock.Text;
        if (_stepPicker!.SelectedDate != null)
            step.EndTime = _stepPicker.SelectedDate!.Value.ToString("d");
        if (_stepCheckBox!.IsChecked != null)
            step.IsDone = (bool)_stepCheckBox.IsChecked;
        model.Update(step);
    }

    private void TodoUpdateClick(object? sender, RoutedEventArgs e)
    {
        if (string.IsNullOrEmpty(TodoBox.Text) || EndPicker.SelectedDate == null) return;
        if (DataContext is not ToDoModel todo) return;
        todo.EndTime = EndPicker.SelectedDate?.Date.ToString("d")!;
        todo.ToDoContext = TodoBox.Text;
        todo.IsDone = (bool)CheckBox.IsChecked!;
        var model = FindModel();
        if (model is null) return;
        model.Update(todo);
    }

    private MainWindowViewModel? FindModel()
    {
        MainWindowViewModel? model = null;
        if (Parent?.DataContext is MainWindowViewModel)
            model = Parent?.DataContext as MainWindowViewModel;
        if (Parent?.Parent?.Parent?.Parent?.Parent?.DataContext is MainWindowViewModel)
            model = Parent?.Parent?.Parent?.Parent?.Parent?.DataContext as MainWindowViewModel;
        return model;
    }

    private void RemoveStepClick(object? sender, RoutedEventArgs e)
    {
        if(sender is not Control control)return;
        if(control.DataContext is not StepModel step)return;
        var model = FindModel();
        if(model == null)return;
        model.RemoveStep(step);
        StepsItems.Items.Remove(step);
    }
}